package cn.springcloud.fix.provider.service;

import cn.hutool.core.util.StrUtil;
import cn.springcloud.fix.common.cache.service.RedisService;
import cn.springcloud.fix.provider.service.TokenService;
import com.aliyun.oss.ServiceException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Slf4j
@Service
@AllArgsConstructor
public class TokenServiceImpl implements TokenService {

    private RedisService redisService;

    @Override
    public String createToken() {
        try {
            String token = UUID.randomUUID().toString();
            redisService.set(token, token, 3600);
            return token;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return null;
    }

    @Override
    public boolean checkToken(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token__");
        if (StrUtil.isBlank(token)) {// header中不存在token
            token = request.getParameter("token__");
            if (StrUtil.isBlank(token)) {// parameter中也不存在token
                throw new Exception("不存在token");
            }
        }
        return true;
    }
}
